package tables

import (
	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
)

// GetMiniAppVersion return the model of table mini_app_version.
func GetMiniAppVersion(ctx *context.Context) (appTable table.Table) {

	appTable = table.NewDefaultTable(table.DefaultConfigWithDriver("postgresql"))

	info := appTable.GetInfo().HideEditButton()
	info.AddField("ID", "id", db.Int).FieldSortable()
	info.AddField("小程序平台", "platform", db.Int).
		FieldDisplay(func(value types.FieldModel) interface{} {
			switch value.Value {
			case "1":
				return "微信"
			case "2":
				return "支付宝"
			case "3":
				return "字节跳动"
			default:
				return "未知平台"
			}
		}).
		FieldFilterable(types.FilterType{FormType: form.SelectSingle, Placeholder: "选择小程序平台"}).
		FieldFilterOptions(types.FieldOptions{
			{Value: "1", Text: "微信"},
			{Value: "2", Text: "支付宝"},
			{Value: "3", Text: "字节跳动"},
		})
	info.AddField("应用ID", "app_id", db.Varchar).FieldCopyable()
	info.AddField("发布版本", "release_version", db.Varchar)
	info.AddField("审核版本", "audit_version", db.Varchar)
	info.AddField("拒绝理由", "reject_reason", db.Varchar)
	info.AddField("状态", "status", db.Varchar).
		FieldDisplay(func(value types.FieldModel) interface{} {
			switch value.Value {
			case "unauthorized":
				return "未授权"
			case "authorized":
				return "已授权"
			case "auditing":
				return "审核中"
			case "reject":
				return "审核拒绝"
			case "released":
				return "已发布"
			default:
				return "未知状态"
			}
		}).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).FieldFilterOptions(types.FieldOptions{
		{Value: "unauthorized", Text: "未授权"},
		{Value: "authorized", Text: "已授权"},
		{Value: "auditing", Text: "审核中"},
		{Value: "reject", Text: "审核拒绝"},
		{Value: "released", Text: "已发布"},
	})
	info.AddField("阶段", "stage", db.Varchar).FieldDisplay(func(value types.FieldModel) interface{} {
		switch value.Value {
		case "initialized":
			return "初始化"
		case "committed":
			return "已构建"
		case "auditing":
			return "审核中"
		case "wait_release":
			return "等待发布"
		case "audit_reject":
			return "审核驳回"
		case "audit_withdrawn":
			return "主动撤回"
		case "released":
			return "已发布"
		default:
			return "未知状态"
		}
	}).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).FieldFilterOptions(types.FieldOptions{
		{Value: "initialized", Text: "初始化"},
		{Value: "committed", Text: "已构建"},
		{Value: "auditing", Text: "审核中"},
		{Value: "wait_release", Text: "等待发布"},
		{Value: "audit_reject", Text: "审核驳回"},
		{Value: "audit_withdrawn", Text: "主动撤回"},
		{Value: "released", Text: "已发布"},
	})

	info.SetTable("mini_app_version").SetTitle("小程序版本信息").SetDescription("小程序版本信息")
	return
}
